

package cn.shengchao.examstar.comment.acl.adapters.repositories;

import cn.shengchao.base.enums.DisEnableStatusEnum;
import cn.shengchao.examstar.comment.acl.ports.repositories.IProductCommentRepository;
import cn.shengchao.examstar.comment.acl.ports.repositories.message.CommentListDto;
import cn.shengchao.examstar.comment.infrastructure.dao.ProductCommentDao;
import cn.shengchao.examstar.comment.infrastructure.dao.po.TProductComment;
import cn.shengchao.examstar.comment.domain.entity.ProductComment;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ProductCommentRepository implements IProductCommentRepository {
    @Autowired
    private ProductCommentDao productCommentDao;

    @Override
    public void save(ProductComment productComment) {
        TProductComment tProductComment = new TProductComment();
        tProductComment.setProductId(productComment.getProductId());
        tProductComment.setStudentId(productComment.getStudent().getId());
        tProductComment.setStudentName(productComment.getStudent().getName());
        tProductComment.setStudentHeadUrl(productComment.getStudent().getHeadUrl());
        tProductComment.setAnonymity(productComment.getAnonymity());
        tProductComment.setContent(productComment.getContent().getValue());
        tProductComment.setScore(productComment.getScore());
        tProductComment.setStatus(productComment.getStatus());
        tProductComment.setReply(productComment.getReply());
        tProductComment.setReviewType(productComment.getReviewType());
        tProductComment.setFilteredComment(productComment.getFilteredComment());
        tProductComment.setSensitiveWordsFound(productComment.getSensitiveWordsFound());
        tProductComment.setReason(productComment.getReason());
        tProductComment.setCreateUser(productComment.getStudent().getId());
        productCommentDao.insert(tProductComment);
    }

    @Override
    public List<CommentListDto> getCommentByProductId(long productId) {
        return productCommentDao.selectList(new LambdaQueryWrapper<TProductComment>()
            .eq(TProductComment::getStatus, DisEnableStatusEnum.ENABLE)
            .eq(TProductComment::getProductId, productId)
            .orderByDesc(TProductComment::getCreateTime)).stream().map(CommentListDto::of).toList();
    }
}
